package com.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.domain.*;
import com.hospital.service.BloodBankService;
import com.hospital.service.BloodCollectionRecordService;
import com.hospital.service.VolunteerService;
import com.hospital.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.el.LambdaExpression;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/bloodCollectionRecord")
@CrossOrigin(origins = "*",maxAge = 3600)
public class BloodCollectionRecordController {
    @Autowired
    private BloodCollectionRecordService bloodCollectionRecordService;

    @Autowired
    private VolunteerService volunteerService;

    @Autowired
    private BloodBankService bloodBankService;

//    @Cacheable(value = "bloodCollectionRecord",key = "#id")
    @GetMapping("/selectPage")
    public R selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                        @RequestParam(defaultValue = "3") Integer pageSize){
        Page<BloodCollectionRecord> page = new Page<>(pageNum, pageSize);
        Page<BloodCollectionRecord> bloodCollectionRecordPage = bloodCollectionRecordService.page(page);
        List<BloodCollectionRecord> records = bloodCollectionRecordPage.getRecords();
        for(BloodCollectionRecord record:records){
            Integer volunteerId = record.getVolunteerId();
            Volunteer volunteer = volunteerService.getById(volunteerId);
            record.setVolunteerName(volunteer.getName());

            Integer bloodBankId = record.getBloodBankId();
            BloodBank bloodBank = bloodBankService.getById(bloodBankId);
            record.setBloodBankName(bloodBank.getName());
        }
        int total = bloodCollectionRecordService.count();
        return R.ok().put("records",records).put("total",total);
    }

//    @Cacheable(value = "bloodCollectionRecord",key = "#id")
    @GetMapping("getBloodTpye")
    public R getBloodTpye(){
        List<BloodCollectionRecord> records = bloodCollectionRecordService.list();
        int[] mount = new int[5];
        for(BloodCollectionRecord record:records){
            if(record.getBloodType().equals("A")){
                mount[0]+=record.getAmount();
            }else if(record.getBloodType().equals("B")){
                mount[1]+=record.getAmount();
            }else if(record.getBloodType().equals("AB")){
                mount[2]+=record.getAmount();
            }else if(record.getBloodType().equals("O")){
                mount[3]+=record.getAmount();
            }else {
                mount[4] += record.getAmount();
            }
        }
        return R.ok().put("data",mount);
    }

//    @CachePut(value = "bloodCollectionRecord",key = "#id")
    @PostMapping("save")
    public R save(@RequestBody BloodCollectionRecord bloodCollectionRecord){
        String bloodBankName = bloodCollectionRecord.getBloodBankName();
        BloodBank bloodBank = bloodBankService.getOne(new QueryWrapper<BloodBank>().eq("name", bloodBankName));
        bloodCollectionRecord.setBloodBankId(bloodBank.getBloodBankId());

        String volunteerName = bloodCollectionRecord.getVolunteerName();
        Volunteer volunteer = volunteerService.getOne(new QueryWrapper<Volunteer>().eq("name", volunteerName));
        bloodCollectionRecord.setVolunteerId(volunteer.getVolunteerId());

        bloodCollectionRecordService.save(bloodCollectionRecord);
        return R.ok();
    }

//    @CachePut(value = "bloodCollectionRecord",key = "#id")
    @PutMapping("/update")
    public R update(@RequestBody BloodCollectionRecord bloodCollectionRecord) {
        String bloodBankName = bloodCollectionRecord.getBloodBankName();
        BloodBank bloodBank = bloodBankService.getOne(new QueryWrapper<BloodBank>().eq("name", bloodBankName));
        bloodCollectionRecord.setBloodBankId(bloodBank.getBloodBankId());

        String volunteerName = bloodCollectionRecord.getVolunteerName();
        Volunteer volunteer = volunteerService.getOne(new QueryWrapper<Volunteer>().eq("name", volunteerName));
        bloodCollectionRecord.setVolunteerId(volunteer.getVolunteerId());

        bloodCollectionRecordService.updateById(bloodCollectionRecord);
        return R.ok();
    }

//    @CacheEvict(value = "bloodCollectionRecord",allEntries = true)
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable int id){
        if(!bloodCollectionRecordService.removeById(id))
            return R.error("删除失败");
        else
            return R.ok("删除成功").put("list",bloodCollectionRecordService.removeById(id));
    }

    @GetMapping("get")
    public R get(){
        List<BloodCollectionRecord> records = bloodCollectionRecordService.list();
        Double[] mount = new Double[7];
        Arrays.fill(mount, 0.0);
        for(BloodCollectionRecord record : records){
            Date date = record.getCollectionDate();
            SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
            String month = monthFormat.format(date);
            if (month.equals("05")) mount[0]+=record.getAmount();
            else if (month.equals("06")) mount[1]+=record.getAmount();
            else if (month.equals("07")) mount[2]+=record.getAmount();
            else if (month.equals("08")) mount[3]+=record.getAmount();
            else if (month.equals("09")) mount[4]+=record.getAmount();
            else if (month.equals("10")) mount[5]+=record.getAmount();
            else if (month.equals("11")) mount[6]+=record.getAmount();
        }
        return R.ok().put("data",mount);
    }
}
